package com.feeyo.groundservice.composewidget.view.button

import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.material3.Icon
import androidx.compose.material3.RadioButton
import androidx.compose.material3.RadioButtonDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import com.feeyo.groundservice.composewidget.R
import com.feeyo.groundservice.composewidget.util.dimenToDp
import com.feeyo.groundservice.composewidget.util.dimenToSp
import com.feeyo.groundservice.composewidget.util.drawableToPainter
import com.feeyo.groundservice.composewidget.util.intervalClick
import com.feeyo.groundservice.composewidget.util.toComposeColor
import com.feeyo.groundservice.composewidget.view.util.HorizontalSpace

/**
 *作者：70893 on 2025-04-24
 *描述：
 */


@Composable
fun RadioButton(
    modifier: Modifier = Modifier,
    title: List<String>,
    defaultSelectIndex: Int,
    selectedIconColor: Color = R.color.blue_2E76EE.toComposeColor(),
    unSelectedIconColor: Color = R.color.gray_D4.toComposeColor(),
    selectedTextColor: Color = R.color.blue_2E76EE.toComposeColor(),
    unSelectedTextColor: Color = R.color.gray_D4.toComposeColor(),
    textSize:TextUnit = R.dimen.d14.dimenToSp(),
    iconSize: Dp = R.dimen.d23.dimenToDp(),
    space: Dp = R.dimen.d10.dimenToDp(),
    onCheckIndex: (Int) -> Unit
) {

    val currentSelectedIndex =
        remember(defaultSelectIndex) { mutableIntStateOf(defaultSelectIndex) }
    Row(
        modifier = Modifier.then(modifier).horizontalScroll(rememberScrollState()),
        horizontalArrangement = Arrangement.spacedBy(space)
    ) {
        title.forEachIndexed { index, s ->
            Row(
                modifier = Modifier.intervalClick {
                    currentSelectedIndex.intValue = index
                    onCheckIndex(index)
                },
                verticalAlignment = Alignment.CenterVertically
            ) {
                RadioButton(
                    selected = currentSelectedIndex.intValue == index,
                    onClick = null,
                    modifier = Modifier.size(iconSize),
                    colors = RadioButtonDefaults.colors().copy(
                        selectedColor = selectedIconColor,
                        unselectedColor = unSelectedIconColor,
                    )
                )
                HorizontalSpace(R.dimen.d5.dimenToDp())
                val color = animateColorAsState(
                    targetValue = if (currentSelectedIndex.intValue == index)
                        selectedTextColor else unSelectedTextColor
                )
                Text(
                    text = s,
                    color = color.value,
                    fontSize = textSize,
                    fontWeight = FontWeight.W400
                )
            }
        }
    }
}


@Composable
fun CustomRadioButton(
    modifier: Modifier = Modifier,
    title: List<String>,
    defaultSelectIndex: Int,
    selectedIcon: Int = R.drawable.icon_radio_check,
    unSelectedIcon: Int = R.drawable.icon_radio_uncheck,
    selectedIconColor: Color = R.color.blue_2E76EE.toComposeColor(),
    unSelectedIconColor: Color = R.color.gray_D4.toComposeColor(),
    selectedTextColor: Color = R.color.blue_2E76EE.toComposeColor(),
    unSelectedTextColor: Color = R.color.gray_D4.toComposeColor(),
    textSize:TextUnit = R.dimen.d14.dimenToSp(),
    iconSize: Dp = R.dimen.d23.dimenToDp(),
    space: Dp = R.dimen.d10.dimenToDp(),
    onCheckIndex: (Int) -> Unit
) {

    val currentSelectedIndex =
        remember(defaultSelectIndex) { mutableIntStateOf(defaultSelectIndex) }
    Row(
        modifier = Modifier.then(modifier).horizontalScroll(rememberScrollState()),
        horizontalArrangement = Arrangement.spacedBy(space)
    ) {
        title.forEachIndexed { index, s ->
            Row(
                modifier = Modifier.intervalClick {
                    currentSelectedIndex.intValue = index
                    onCheckIndex(index)
                },
                verticalAlignment = Alignment.CenterVertically
            ) {
                val checked = currentSelectedIndex.intValue == index
                val iconColor = animateColorAsState(
                    targetValue = if (checked)
                        selectedIconColor else unSelectedIconColor
                )

                val textColor = animateColorAsState(
                    targetValue = if (checked)
                        selectedTextColor else unSelectedTextColor
                )

                Icon(
                    painter = if (checked) selectedIcon.drawableToPainter() else unSelectedIcon.drawableToPainter(),
                    contentDescription = null,
                    modifier = Modifier.size(iconSize),
                    tint = iconColor.value
                )

                HorizontalSpace(R.dimen.d5.dimenToDp())

                Text(
                    text = s,
                    color = textColor.value,
                    fontSize = textSize,
                    fontWeight = FontWeight.W400
                )
            }
        }
    }
}

